﻿using AxMSTSCLib;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace RdpClient
{
	public class Form_Main : Form
	{
		private IContainer components = null;
		private ToolStrip tool_Main;
		private ToolStripDropDownButton tsb_Rdps;
		private ToolStripButton tsb_Config;
		private ToolStripButton tsb_About;
		private TabControl tab_Rdps;
		private AxMsRdpClient8 rdpClient;
		private ToolStripButton tsb_Disconnect;
		private ToolStripButton tsb_DisconnectAll;
		private ToolStripSeparator toolStripSeparator1;
		private ToolStripSeparator toolStripSeparator2;
		private ToolStripButton tsb_Max;
		private ToolStripSeparator toolStripSeparator3;
		public Form_Main()
		{
			this.InitializeComponent();
		}
		private void BindMenuItem()
		{
			this.tsb_Rdps.DropDownItems.Clear();
			if (Config.RdpInfos != null)
			{
				RdpInfo[] items = Config.RdpInfos.Items;
				for (int i = 0; i < items.Length; i++)
				{
					RdpInfo rdpInfo = items[i];
					ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem();
					toolStripMenuItem.ImageScaling = ToolStripItemImageScaling.SizeToFit;
					toolStripMenuItem.Name = string.Format("MenuItem{0}", rdpInfo.AliasName);
					toolStripMenuItem.Text = rdpInfo.AliasName;
					toolStripMenuItem.Tag = rdpInfo;
					this.tsb_Rdps.DropDownItems.Add(toolStripMenuItem);
				}
			}
		}
		public void RdpConnect(RdpInfo info)
		{
			try
			{
				RdpTabPage rdpTabPage = new RdpTabPage();
				rdpTabPage.Text = info.AliasName;
				foreach (TabPage tabPage in this.tab_Rdps.TabPages)
				{
					if (tabPage.Text == rdpTabPage.Text)
					{
						this.tab_Rdps.SelectTab(tabPage);
						return;
					}
				}
				this.tab_Rdps.TabPages.Add(rdpTabPage);
				rdpTabPage.Connect(info);
				this.tab_Rdps.SelectTab(rdpTabPage);
			}
			catch (Exception ex)
			{
				MessageBox.Show("消息内容", ex.Message, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error);
			}
		}
		private void Form_Main_Load(object sender, EventArgs e)
		{
			this.rdpClient.Visible = false;
			Config.Load();
			this.BindMenuItem();
		}
		private void Form_Main_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (MessageBox.Show("确定退出程序？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
			{
				e.Cancel = true;
			}
		}
		private void tsb_Rdps_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
		{
			try
			{
				RdpInfo rdpInfo = e.ClickedItem.Tag as RdpInfo;
				if (rdpInfo != null)
				{
					this.RdpConnect(rdpInfo);
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show("消息内容", ex.Message, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error);
			}
		}
		private void tsb_Disconnect_Click(object sender, EventArgs e)
		{
			try
			{
				if (this.tab_Rdps.SelectedIndex >= 0)
				{
					RdpTabPage rdpTabPage = (RdpTabPage)this.tab_Rdps.SelectedTab;
					if (rdpTabPage != null)
					{
						rdpTabPage.Disconnect();
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show("消息内容", ex.Message, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error);
			}
		}
		private void tsb_DisconnectAll_Click(object sender, EventArgs e)
		{
			try
			{
				foreach (TabPage tabPage in this.tab_Rdps.TabPages)
				{
					RdpTabPage rdpTabPage = (RdpTabPage)tabPage;
					rdpTabPage.Disconnect();
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show("消息内容", ex.Message, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error);
			}
		}
		private void tsb_Max_Click(object sender, EventArgs e)
		{
			try
			{
				if (this.tab_Rdps.SelectedIndex >= 0)
				{
					RdpTabPage rdpTabPage = (RdpTabPage)this.tab_Rdps.SelectedTab;
					if (rdpTabPage != null)
					{
						AxMsRdpClient9 axMsRdpClient = rdpTabPage.Controls[0] as AxMsRdpClient9;
						axMsRdpClient.FullScreen = true;
						axMsRdpClient.FullScreenTitle = rdpTabPage.Text;
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show("消息内容", ex.Message, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error);
			}
		}
		private void tsb_Config_Click(object sender, EventArgs e)
		{
			Form_Settings form_Settings = new Form_Settings();
			if (form_Settings.ShowDialog() == DialogResult.OK)
			{
				this.BindMenuItem();
			}
		}
		private void tsb_About_Click(object sender, EventArgs e)
		{
			MessageBox.Show("远程桌面", "信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
		}
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form_Main));
            this.tool_Main = new System.Windows.Forms.ToolStrip();
            this.tsb_Rdps = new System.Windows.Forms.ToolStripDropDownButton();
            this.tsb_Disconnect = new System.Windows.Forms.ToolStripButton();
            this.tsb_DisconnectAll = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
            this.tsb_Max = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
            this.tsb_Config = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
            this.tsb_About = new System.Windows.Forms.ToolStripButton();
            this.tab_Rdps = new System.Windows.Forms.TabControl();
            this.rdpClient = new AxMSTSCLib.AxMsRdpClient8();
            this.tool_Main.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.rdpClient)).BeginInit();
            this.SuspendLayout();
            // 
            // tool_Main
            // 
            this.tool_Main.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.tsb_Rdps,
            this.tsb_Disconnect,
            this.tsb_DisconnectAll,
            this.toolStripSeparator3,
            this.tsb_Max,
            this.toolStripSeparator1,
            this.tsb_Config,
            this.toolStripSeparator2,
            this.tsb_About});
            this.tool_Main.Location = new System.Drawing.Point(0, 0);
            this.tool_Main.Name = "tool_Main";
            this.tool_Main.Size = new System.Drawing.Size(784, 25);
            this.tool_Main.TabIndex = 0;
            // 
            // tsb_Rdps
            // 
            this.tsb_Rdps.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.tsb_Rdps.Name = "tsb_Rdps";
            this.tsb_Rdps.Size = new System.Drawing.Size(69, 22);
            this.tsb_Rdps.Text = "远程列表";
            this.tsb_Rdps.DropDownItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.tsb_Rdps_DropDownItemClicked);
            // 
            // tsb_Disconnect
            // 
            this.tsb_Disconnect.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.tsb_Disconnect.Name = "tsb_Disconnect";
            this.tsb_Disconnect.Size = new System.Drawing.Size(60, 22);
            this.tsb_Disconnect.Text = "关闭当前";
            this.tsb_Disconnect.Click += new System.EventHandler(this.tsb_Disconnect_Click);
            // 
            // tsb_DisconnectAll
            // 
            this.tsb_DisconnectAll.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.tsb_DisconnectAll.Name = "tsb_DisconnectAll";
            this.tsb_DisconnectAll.Size = new System.Drawing.Size(60, 22);
            this.tsb_DisconnectAll.Text = "关闭所有";
            this.tsb_DisconnectAll.Click += new System.EventHandler(this.tsb_DisconnectAll_Click);
            // 
            // toolStripSeparator3
            // 
            this.toolStripSeparator3.Name = "toolStripSeparator3";
            this.toolStripSeparator3.Size = new System.Drawing.Size(6, 25);
            // 
            // tsb_Max
            // 
            this.tsb_Max.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.tsb_Max.Name = "tsb_Max";
            this.tsb_Max.Size = new System.Drawing.Size(48, 22);
            this.tsb_Max.Text = "最大化";
            this.tsb_Max.Click += new System.EventHandler(this.tsb_Max_Click);
            // 
            // toolStripSeparator1
            // 
            this.toolStripSeparator1.Name = "toolStripSeparator1";
            this.toolStripSeparator1.Size = new System.Drawing.Size(6, 25);
            // 
            // tsb_Config
            // 
            this.tsb_Config.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.tsb_Config.Name = "tsb_Config";
            this.tsb_Config.Size = new System.Drawing.Size(36, 22);
            this.tsb_Config.Text = "设置";
            this.tsb_Config.Click += new System.EventHandler(this.tsb_Config_Click);
            // 
            // toolStripSeparator2
            // 
            this.toolStripSeparator2.Name = "toolStripSeparator2";
            this.toolStripSeparator2.Size = new System.Drawing.Size(6, 25);
            // 
            // tsb_About
            // 
            this.tsb_About.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.tsb_About.Name = "tsb_About";
            this.tsb_About.Size = new System.Drawing.Size(36, 22);
            this.tsb_About.Text = "关于";
            this.tsb_About.Click += new System.EventHandler(this.tsb_About_Click);
            // 
            // tab_Rdps
            // 
            this.tab_Rdps.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tab_Rdps.Location = new System.Drawing.Point(0, 25);
            this.tab_Rdps.Name = "tab_Rdps";
            this.tab_Rdps.SelectedIndex = 0;
            this.tab_Rdps.Size = new System.Drawing.Size(784, 537);
            this.tab_Rdps.TabIndex = 1;
            // 
            // rdpClient
            // 
            this.rdpClient.Enabled = true;
            this.rdpClient.Location = new System.Drawing.Point(0, 516);
            this.rdpClient.Name = "rdpClient";
            this.rdpClient.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("rdpClient.OcxState")));
            this.rdpClient.Size = new System.Drawing.Size(192, 192);
            this.rdpClient.TabIndex = 2;
            // 
            // Form_Main
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(784, 562);
            this.Controls.Add(this.rdpClient);
            this.Controls.Add(this.tab_Rdps);
            this.Controls.Add(this.tool_Main);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MinimumSize = new System.Drawing.Size(800, 600);
            this.Name = "Form_Main";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "远程桌面";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form_Main_FormClosing);
            this.Load += new System.EventHandler(this.Form_Main_Load);
            this.tool_Main.ResumeLayout(false);
            this.tool_Main.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.rdpClient)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
	}
}
